import {
  navigateTo
} from "../../utils/util.js";
import Request from "../../utils/request";
let app = getApp();
let store = getApp().store;

Component({
  /**
   * 组件的属性列表
   */
  properties: {
      // subData: {
      //     type: Array
      // }
  },

  /**
   * 组件的初始数据
   */
  data: {
    // imgBaseUrl: app.globalData.ENVIRONMENT_CONFIG.imgBaseUrl.split('/static')[0] + '/static/img/',
    // menus: app.globalData.ENVIRONMENT_CONFIG.subMenus
      imgBaseUrl: '',
      menus: []
  },

    ready(){
      if(wx.getStorageSync('openid')){
        this.doMenu();
      }else{
        wx.login({
          success: wxRes => {
            Request.postRequest('/wxAppletPay/getOpenId', { code: wxRes.code }).then(res => {
              wx.setStorageSync('openid', JSON.parse(res.data.openId).openid)
              this.doMenu();
            }).catch(e => {
              console.log(e)
              this.doMenu();
          })      
          }
        })
      }
    },
  /**
   * 组件的方法列表
   */
  methods: {
    goPage(event) {
      let dataset = event.currentTarget.dataset
      let url = dataset.url
      let needLogin = dataset.needlogin
      // 若未设置跳转地址
      if (!url) {
        wx.showToast({
          title: '系统建设中',
          icon: 'none',
          duration: 2000
        })
        return
      }
      if (dataset.disable) {
        wx.showToast({
          title: '抱歉，还未上线',
          icon: 'none',
          duration: 2000
        })
        return
      }
      if (dataset.openmini) {
        wx.navigateToMiniProgram({
          appId: dataset.appid,
          path: dataset.path,
          extraData: {
            foo: 'bar'
          },
          envVersion: 'release',
          success(res) {
            // 打开成功
          }
        })
        return
      }
      if (needLogin) {
        navigateTo({
          url: url
        });
      } else {
        wx.navigateTo({
          url: url
        });
      }
    },
       
  doMenu() {
    let userRoleList =  store.getState().systemLoginData.roleList || wx.getStorageSync('roleList');
    let _that = this;
    let subData = [];
    Request.getRequest('/menu/page',{orderBy:'rank',pageNo:1,pageSize:1000}).then(res => {
        let list = res.data;
        for(let i = 0; i < list.length; i++){
            let item = list[i];
            if(item.isShow ===0){
                continue;
            }
            if(item.isMain == 0){
                if(null != item.roleIdList && item.roleIdList != ""){
                    console.log("item", item);
                    let hasRole = false;
                    let roleIdListArr = item.roleIdList.split(",");
                    console.log("roleIdListArr", roleIdListArr);
                    console.log("userRoleList", userRoleList);
                    for(let j = 0; j < roleIdListArr.length; j++){
                        let roleId = roleIdListArr[j];
                        for(let k = 0; k < userRoleList.length; k++){
                            let userRole = userRoleList[k];
                            if(userRole == roleId){
                                hasRole = true;
                            }
                        }
                    }
                    if(!hasRole){
                        continue;
                    }
                }
                if(item.skipUrl){
                  console.log('getStorageSync openid',wx.getStorageSync('openid')) 
                  let strUrl = item.skipUrl;
                  strUrl = strUrl.replace(/{{openid}}/, wx.getStorageSync('openid') )
                  item.skipUrl=strUrl;
                }
                let subItem = {
                    title: item.title,
                    subTitle: item.menuDesc,
                    img: item.pictureUrl,
                    url: item.skipUrl,
                    appid: item.appid,
                    openMini: (item.openMini === 1),
                    needLogin: (item.needLogin === 1),
                    path: item.skipUrl,
                };
                if(null != item.pictureParam && item.pictureParam != ''){
                    subItem.img += item.pictureParam;
                }
                subData.push(subItem);
            }
        }
        console.log('subData subData',subData) 
        _that.setData({
            menus: subData
        })
    })
  },
  }
})
